package com.ruoyi.official.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.official.domain.OfficialNews;
import com.ruoyi.official.mapper.OfficialNewsMapper;
import com.ruoyi.official.service.IOfficialNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 新闻Service业务层处理
 *
 * @author yuanqiji
 * @date 2025-03-21
 */
@Service
public class OfficialNewsServiceImpl implements IOfficialNewsService {
    @Autowired
    private OfficialNewsMapper officialNewsMapper;

    /**
     * 查询新闻
     *
     * @param id 新闻主键
     * @return 新闻
     */
    @Override
    public OfficialNews selectOfficialNewsById(Long id) {
        return officialNewsMapper.selectOfficialNewsById(id);
    }

    /**
     * 查询新闻列表
     *
     * @param officialNews 新闻
     * @return 新闻
     */
    @Override
    public List<OfficialNews> selectOfficialNewsList(OfficialNews officialNews) {
        return officialNewsMapper.selectOfficialNewsList(officialNews);
    }

    /**
     * 新增新闻
     *
     * @param officialNews 新闻
     * @return 结果
     */
    @Override
    public int insertOfficialNews(OfficialNews officialNews) {
        officialNews.setCreateTime(DateUtils.getNowDate());
        return officialNewsMapper.insertOfficialNews(officialNews);
    }

    /**
     * 修改新闻
     *
     * @param officialNews 新闻
     * @return 结果
     */
    @Override
    public int updateOfficialNews(OfficialNews officialNews) {
        officialNews.setUpdateTime(DateUtils.getNowDate());
        return officialNewsMapper.updateOfficialNews(officialNews);
    }

    /**
     * 批量删除新闻
     *
     * @param ids 需要删除的新闻主键
     * @return 结果
     */
    @Override
    public int deleteOfficialNewsByIds(Long[] ids) {
        return officialNewsMapper.deleteOfficialNewsByIds(ids);
    }

    /**
     * 删除新闻信息
     *
     * @param id 新闻主键
     * @return 结果
     */
    @Override
    public int deleteOfficialNewsById(Long id) {
        return officialNewsMapper.deleteOfficialNewsById(id);
    }

    @Override
    public void addVisitNumber(Long id) {
        OfficialNews officialNews = officialNewsMapper.selectOfficialNewsById(id);
        officialNews.setVisitNumber(officialNews.getVisitNumber() + 1);
        officialNewsMapper.updateOfficialNews(officialNews);
    }
}
